﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web.Http;
using WorkBasket.Data.Entities;
using WorkBasket.Infrastructure;
using WorkBasket.Data.Repository;
using WorkBasket.Models;
 

namespace WorkBasket.Controllers.Api
{
    [Authorize]
    public class WorkflowController : ApiController
    {
        private SOC soc = new SOC(new MyContext());
        [Route("api/workflow/getall")]
        [HttpGet]
        public IHttpActionResult GetAll()
        {
            return Json(soc.Workflows.GetAll());
        }

        public string Get(int id)
        {
            return "value";
        }



        [HttpPost]
        [Route("api/workflow/save")]
        public IHttpActionResult Post([FromBody] Workflow value)
        {
            try
            {
 
                if (value.WorkflowID == 0)
                {
                    soc.Workflows.Add(value);
                    ProcessVersion pv = new ProcessVersion();
                    pv.EffectiveDate = DateTime.Now.ToUniversalTime();
                    pv.VersionName = "1.0";
                    pv.WorkflowID = value.WorkflowID;

                    soc.ProcessVersions.Add(pv);
                }
                else
                {
                    soc.Workflows.Update(value);
                }

                soc.CommitChanges();

                return Json(new DataReturn("Workflow Saved", value));
            }
            catch (Exception e)
            {
                return Json(new DataReturn(e.Message, e));
            }
        }


        [HttpPost]
        [Route("api/workflow/delete")]
        public IHttpActionResult Delete([FromBody] Workflow value)
        {
            try
            {
                soc.Workflows.Delete(value.WorkflowID);
                soc.CommitChanges();
                return Json(new DataReturn("Workflow Deleted", value));
            }
            catch (Exception e)
            {
                return Json(new DataReturn(e.Message, e));
            }
        }
    }
}
